package com.htthorizon.powersupply.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htthorizon.powersupply.domain.TgLoadItem;
import com.htthorizon.powersupply.search.TgLoadItemSearch;
import com.htthorizon.powersupply.service.TgLoadItemService;
import com.htthorizon.powersupply.utils.OrgUtil;
import com.htthorizon.powersupply.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/tgLoadItem")
@Slf4j
public class TgLoadItemController {

    @Autowired
    private TgLoadItemService tgLoadItemService;

    @GetMapping("/page")
    public R<Map<String, List<com.htthorizon.powersupply.dto.TgLoadItem>>> page(@ModelAttribute TgLoadItemSearch search) {
        LambdaQueryWrapper<TgLoadItem> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(search.getTgNm()), TgLoadItem::getTgNm, search.getTgNm());
        wrapper.like(StringUtils.isNotEmpty(search.getStdCountyOrgNm()), TgLoadItem::getStdCountyOrgNm, search.getStdCountyOrgNm());

        List<com.htthorizon.powersupply.dto.TgLoadItem> list = tgLoadItemService.page1(wrapper);
        for(com.htthorizon.powersupply.dto.TgLoadItem tg:list){
            tg.setStdCountyOrgNm(map(tg.getStdCountyOrgNm()));
            tg.setLoadFactor(tg.getNewLoadFactor());
            tg.setStdOrgNm(OrgUtil.getStdOrgNm(tg.getStdOrgNm()));
        }
        Map<String, List<com.htthorizon.powersupply.dto.TgLoadItem>> collect = list.stream().collect(Collectors.groupingBy(TgLoadItemController::changeName));
        collect.putIfAbsent("heavyload", new ArrayList<>());
        collect.putIfAbsent("overload", new ArrayList<>());
        collect.putIfAbsent("suspended", new ArrayList<>());

        return R.ok(collect);
    }

    private static String changeName(com.htthorizon.powersupply.dto.TgLoadItem tgLoadItem) {
        String loadType = tgLoadItem.getLoadType();
        switch (loadType) {
            case "重载":
                return "heavyload";
            case "过载":
                return "overload";
            case "停运":
                return "suspended";
            default:
                return "4";
        }
    }

    private String map(String tgNm){
        if (tgNm.contains("城东")) {
            return "城东";
        } else if (tgNm.contains("天元")) {
            return "天元";
        }else if (tgNm.contains("渌口")) {
            return "渌口";
        } else if (tgNm.contains("醴陵")) {
            return "醴陵";
        } else if (tgNm.contains("攸县")) {
            return "攸县";
        } else if (tgNm.contains("茶陵")) {
            return "茶陵";
        } else if (tgNm.contains("炎陵")) {
            return "炎陵";
        }else {
            return tgNm;
        }
    }


}
